package instances;

import java.util.*;

/*
 * 游戏房间-->牌的操作类
 * */
public class Room {
    //1、准备好牌
    private List<Card> allCards = new ArrayList<>();

    //2、初始化牌，实例代码块
    {
        //3.准备花色
        String[] colors = {"♦", "♣", "♥", "♠"};
        //4.准备点数
        String[] sizes = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
        int num = 0;
        //5、遍历，组合所有花色和集合
        for (String size : sizes) {
            num++;
            for (String color : colors) {
                Card card = new Card(size, color, num);
                allCards.add(card);
            }
        }
        //8、添加大小王
        Collections.addAll(allCards, new Card("", "🃏", ++num), new Card("", "👲", ++num));
    }

    /*
     * 启动游戏
     * */
    public void start() {

        //9、洗牌
        System.out.println("洗牌前" + allCards);
        Collections.shuffle(allCards);
        System.out.println("洗牌后" + allCards);
        //10、发牌-》创造三个玩家
        Map<String, List<Card>> player = new HashMap<>();
        List<Card> xm = new ArrayList<>();
        player.put("小明", xm);
        List<Card> xh = new ArrayList<>();
        player.put("小虹", xh);
        List<Card> xb = new ArrayList<>();
        player.put("小北", xb);
        for (int i = 0; i < allCards.size() - 3; i++) {
            Card card = allCards.get(i);
            if (i % 3 == 0) {
                xm.add(card);
            } else if (i % 3 == 1) {
                xh.add(card);
            } else if (i % 3 == 2) {
                xb.add(card);
            }
        }



        //11、最后三张牌
        List<Card> lastCards = allCards.subList(allCards.size() - 3, allCards.size());
        Random random=new Random();
        int x=random.nextInt(1,4);
        if(x==1){
            xb.addAll(lastCards);
        } else if (x==2) {
            xm.addAll(lastCards);
        }else if (x==3) {
            xh.addAll(lastCards);
        }


        //12、排序
        sortCards(xh);
        sortCards(xm);
        sortCards(xb);
        //13、看牌
        Set<Map.Entry<String, List<Card>>> entries = player.entrySet();
        for (Map.Entry<String, List<Card>> entry : entries) {
            String name = entry.getKey();
            List<Card> cards = entry.getValue();
            System.out.println(name + "的牌是:" + cards);
        }
    }

    private void sortCards(List<Card> cards) {
        cards.sort((c1, c2) -> c2.getNum() - c1.getNum());
    }


}
